home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / PPCToolboxKeychain / MIB Bits / MoreAppleEvents.cp < prev    next >
Encoding:
Text File  |  2000-10-06  |  40.7 KB  |  1,319 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MoreAppleEvents.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by:    Andy Bachorski
  7.  
  8.     Copyright:    Copyright (c) 2000 Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.  
  20.         <11>     27/3/00    Quinn   Remove MoreAEDeleteItemFromRecord.  It's functionality is
  21.                                     covered by AEDeleteKeyDesc.
  22.         <10>     20/3/00    Quinn   Added routines to deal with "missing value".  Added
  23.                                     MoreAECopyDescriptorDataToHandle.  Added
  24.                                     MoreAEDeleteItemFromRecord.
  25.          <9>      3/9/00    gaw     Y2K!
  26.          <8>      3/9/00    gaw     API changes for MoreAppleEvents
  27.          <7>      3/9/00    GW      Intergrating AppleEvent Helper code. First Check In
  28.          <6>      6/3/00    Quinn   Added a bunch of trivial wrapper routines.  George may come
  29.                                     along and change all these soon, but I needed them for MoreOSL.
  30.          <5>      1/3/00    Quinn   Change the signature for AEGetDescData to match the version we
  31.                                     actually shipped.
  32.          <4>     2/15/99    PCG     add AEGetDescDataSize for non-Carbon clients
  33.          <3>     1/29/99    PCG     add AEGetDescData
  34.          <2>    11/11/98    PCG     fix header
  35.          <1>    11/10/98    PCG     first big re-org at behest of Quinn
  36.  
  37.     Old Change History (most recent first):
  38.  
  39.          <2>    10/11/98    Quinn   Convert "MorePrefix.h" to "MoreSetup.h".
  40.          <2>     6/16/98    PCG     CreateProcessTarget works with nil PSN
  41.          <1>     6/16/98    PCG     initial checkin
  42. */
  43.  
  44. //    Conditionals to setup the build environment the way we like it.
  45. #include "MoreSetup.h"
  46. //**********    Universal Headers        ****************************************
  47. #include <AERegistry.h>
  48. #include <AEObjects.h>
  49. #include <AEPackObject.h>
  50. #include <ASRegistry.h>
  51. //#include <FinderRegistry.h>
  52. #include <Gestalt.h>
  53. //**********    Project Headers            ****************************************
  54. #include "MoreAppleEvents.h"
  55. #include "MoreAEObjects.h"
  56. #include "MoreProcesses.h"
  57. #include "MoreMemory.h"
  58. //**********    Private Definitions        ****************************************
  59. enum {
  60.     kFinderFileType            = 'FNDR',
  61.     kFinderCreatorType        = 'MACS',
  62.     kFinderProcessType        = 'FNDR',
  63.     kFinderProcessSignature    = 'MACS'
  64. };
  65. static AEIdleUPP gAEIdleUPP = nil;
  66. //*******************************************************************************
  67. #pragma mark ==> Create Target Descriptors for AEvents •
  68. /********************************************************************************
  69.     Create and return an AEDesc for the process target with the specified PSN.
  70.     If no PSN is supplied the use the current process
  71.  
  72.     pAEEventClass        input:    The class of the event to be created.
  73.     pAEEventID            input:    The ID of the event to be created.
  74.     pAppleEvent        input:    Pointer to an AppleEvent where the
  75.                             event record will be returned.
  76.                     output:    The Apple event.
  77.     
  78.     RESULT CODES
  79.     ____________
  80.     noErr               0    No error    
  81.     memFullErr        -108    Not enough room in heap zone    
  82.     ____________
  83. */
  84. pascal OSErr MoreAECreateProcessTarget (ProcessSerialNumber *pPSN, AEDesc *target)
  85. {
  86.     ProcessSerialNumber self;
  87.  
  88.     if (!pPSN)
  89.     {
  90.         pPSN = &self;
  91.  
  92.         self.lowLongOfPSN        = kNoProcess;
  93.         self.highLongOfPSN        = kCurrentProcess;
  94.     }
  95.  
  96.     return AECreateDesc (typeProcessSerialNumber,pPSN,sizeof(*pPSN),target);
  97. }
  98. //*******************************************************************************
  99. #pragma mark ==> Create AEvents •
  100. /********************************************************************************
  101.     Create and return an AppleEvent of the given class and ID. The event will be
  102.     targeted at the current process, with an AEAddressDesc of type
  103.     typeProcessSerialNumber.
  104.  
  105.     pAEEventClass        input:    The class of the event to be created.
  106.     pAEEventID            input:    The ID of the event to be created.
  107.     pAppleEvent        input:    Pointer to an AppleEvent where the
  108.                             event record will be returned.
  109.                     output:    The Apple event.
  110.     
  111.     RESULT CODES
  112.     ____________
  113.     noErr               0    No error    
  114.     memFullErr        -108    Not enough room in heap zone    
  115.     ____________
  116. */
  117. pascal OSErr MoreAECreateAppleEventSelfTarget( const AEEventClass pAEEventClass,
  118.                               const AEEventID pAEEventID,
  119.                               AppleEvent *pAppleEvent )
  120. {
  121.     OSErr    anErr = noErr;
  122.     
  123.     ProcessSerialNumber        selfPSN = {0, kCurrentProcess};
  124.     
  125.     anErr = MoreAECreateAppleEventProcessTarget( &selfPSN, pAEEventClass, pAEEventID, pAppleEvent );
  126.     
  127.     return ( anErr );
  128. }//end MoreAECreateAppleEventSelfTarget
  129. /********************************************************************************
  130.     Create and return an AppleEvent of the given class and ID. The event will be
  131.     targeted at the process specified by the target type and creator codes, 
  132.     with an AEAddressDesc of type typeProcessSerialNumber.
  133.  
  134.     pType        input:    The file type of the process to be found.
  135.     pCreator    input:    The creator type of the process to be found.
  136.     pAEEventClass        input:    The class of the event to be created.
  137.     pAEEventID            input:    The ID of the event to be created.
  138.     pAppleEvent        input:    Pointer to an AppleEvent where the
  139.                             event record will be returned.
  140.                     output:    The Apple event.
  141.     
  142.     RESULT CODES
  143.     ____________
  144.     noErr               0    No error    
  145.     memFullErr        -108    Not enough room in heap zone    
  146.     procNotFound    –600    No eligible process with specified descriptor
  147.     ____________
  148. */
  149. pascal    OSErr    MoreAECreateAppleEventSignatureTarget(const OSType pType,
  150.                                                   const OSType pCreator,
  151.                                                   const AEEventClass pAEEventClass,
  152.                                                   const AEEventID pAEEventID,
  153.                                                         AppleEvent *pAppleEvent )
  154. {
  155.     OSErr    anErr = noErr;
  156.     
  157.     ProcessSerialNumber        psn = {kNoProcess, kNoProcess};
  158.     
  159.     anErr = MoreProcFindProcessBySignature( pType, pCreator, &psn );
  160.     if ( anErr == noErr )
  161.     {
  162.         anErr = MoreAECreateAppleEventProcessTarget( &psn, pAEEventClass, pAEEventID, pAppleEvent );
  163.     }
  164.     return anErr;
  165. }//end MoreAECreateAppleEventSignatureTarget
  166. /********************************************************************************
  167.     Create and return an AppleEvent of the given class and ID. The event will be
  168.     targeted at the application with the specific creator.
  169.  
  170.     psnPtr            input:    Pointer to the PSN to target the event with.
  171.     pAEEventClass    input:    The class of the event to be created.
  172.     pAEEventID        input:    The ID of the event to be created.
  173.     pAppleEvent        input:    Pointer to an AppleEvent where the
  174.                             event record will be returned.
  175.                     output:    The Apple event.
  176.     
  177.     RESULT CODES
  178.     ____________
  179.     noErr               0    No error    
  180.     memFullErr        -108    Not enough room in heap zone    
  181.     procNotFound    –600    No eligible process with specified descriptor
  182.     ____________
  183. */
  184. pascal OSStatus MoreAECreateAppleEventCreatorTarget(
  185.                             const AEEventClass pAEEventClass,
  186.                             const AEEventID pAEEventID,
  187.                             const OSType pCreator,
  188.                             AppleEvent *pAppleEvent)
  189. {
  190.     OSStatus     err;
  191.     AEDesc         targetDesc;
  192.     
  193.     MoreAssertQ(pAppleEvent != nil);
  194.  
  195.     MoreAENullDesc(&targetDesc);
  196.     
  197.     err = AECreateDesc(typeApplSignature, &pCreator, sizeof(pCreator), &targetDesc);
  198.     if (err == noErr)
  199.         err = AECreateAppleEvent(pAEEventClass, pAEEventID, &targetDesc, 
  200.                                     kAutoGenerateReturnID, kAnyTransactionID, pAppleEvent);
  201.     MoreAEDisposeDesc(&targetDesc);
  202.     
  203.     return err;
  204. }
  205.  
  206. /********************************************************************************
  207.     Create and return an AppleEvent of the given class and ID. The event will be
  208.     targeted with the provided PSN.
  209.  
  210.     psnPtr            input:    Pointer to the PSN to target the event with.
  211.     pAEEventClass        input:    The class of the event to be created.
  212.     pAEEventID            input:    The ID of the event to be created.
  213.     pAppleEvent        input:    Pointer to an AppleEvent where the
  214.                             event record will be returned.
  215.                     output:    The Apple event.
  216.     
  217.     RESULT CODES
  218.     ____________
  219.     noErr               0    No error    
  220.     memFullErr        -108    Not enough room in heap zone    
  221.     procNotFound    –600    No eligible process with specified descriptor
  222.     ____________
  223. */
  224. pascal    OSErr    MoreAECreateAppleEventProcessTarget( const ProcessSerialNumberPtr psnPtr,
  225.                                            const AEEventClass pAEEventClass,
  226.                                            const AEEventID pAEEventID,
  227.                                                  AppleEvent *pAppleEvent )
  228. {
  229.     OSErr    anErr = noErr;
  230.     AEDesc    targetAppDesc = {typeNull, nil};
  231.     
  232.     anErr = AECreateDesc (typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &targetAppDesc);
  233.  
  234.     if ( anErr == noErr )
  235.     {
  236.         anErr = AECreateAppleEvent( pAEEventClass, pAEEventID, &targetAppDesc,
  237.                                     kAutoGenerateReturnID, kAnyTransactionID, pAppleEvent);
  238.     }
  239.     
  240.     AEDisposeDesc( &targetAppDesc );
  241.     
  242.     return anErr;
  243. }//end MoreAECreateAppleEventProcessTarget
  244. /********************************************************************************
  245.     Create and return an AppleEvent of the given class and ID. The event will be
  246.     targeted with the provided TargetID.
  247.  
  248.     pTargetID        input:    Pointer to the TargetID to target the event with.
  249.     pAEEventClass        input:    The class of the event to be created.
  250.     pAEEventID            input:    The ID of the event to be created.
  251.     pAppleEvent        input:    Pointer to an AppleEvent where the
  252.                             event record will be returned.
  253.                     output:    The Apple event.
  254.     
  255.     RESULT CODES
  256.     ____________
  257.     noErr               0    No error    
  258.     memFullErr        -108    Not enough room in heap zone    
  259.     procNotFound    –600    No eligible process with specified descriptor
  260.     ____________
  261. */
  262. pascal    OSErr    MoreAECreateAppleEventTargetID( const TargetID *pTargetID,
  263.                                       const AEEventClass pAEEventClass,
  264.                                       const AEEventID pAEEventID,
  265.                                             AppleEvent *pAppleEvent )
  266. {
  267.     OSErr    anErr = noErr;
  268.     AEDesc    targetAppDesc = {typeNull, nil};
  269.     
  270.     anErr = AECreateDesc (typeTargetID, pTargetID, sizeof( TargetID ), &targetAppDesc);
  271.  
  272.     if ( anErr == noErr )
  273.     {
  274.         anErr = AECreateAppleEvent( pAEEventClass, pAEEventID, &targetAppDesc,
  275.                                     kAutoGenerateReturnID, kAnyTransactionID, pAppleEvent);
  276.     }
  277.     
  278.     AEDisposeDesc( &targetAppDesc );
  279.     
  280.     return anErr;
  281. }//end MoreAECreateAppleEventTargetID
  282.  
  283. #pragma mark ==> Send AppleEvents •
  284. #if 0
  285. //•    De-appreciated! Don't use! Use one of the more specific routines (w/idle proc) below.
  286. pascal OSErr MoreAESendAppleEvent (const AppleEvent *pAppleEvent, AppleEvent *pAEReply)
  287. {
  288.     OSErr err = noErr;
  289.  
  290.     AESendMode aeSendMode = kAEAlwaysInteract | kAECanSwitchLayer;
  291.  
  292.     if (pAEReply)
  293.     {
  294.         aeSendMode |= kAEWaitReply;
  295.  
  296.         pAEReply->descriptorType    = typeNull;
  297.         pAEReply->dataHandle        = nil;
  298.     }
  299.  
  300.     err = AESend (pAppleEvent, pAEReply, aeSendMode, kAENormalPriority, kAEDefaultTimeout, nil, nil);
  301.  
  302.     return err;
  303. }
  304. #endif 0
  305. /********************************************************************************
  306.     Send the provided AppleEvent using the provided idle function.
  307.     Will wait for a reply if an idle function is provided, but no result will be returned.
  308.  
  309.     pIdleProcUPP    input:    The idle function to use when sending the event.
  310.     pAppleEvent        input:    The event to be sent.
  311.     
  312.     RESULT CODES
  313.     ____________
  314.     noErr               0    No error    
  315.     
  316.     and any other error that can be returned by AESend or the handler
  317.     in the application that gets the event.
  318.     ____________
  319. */
  320. pascal    OSErr    MoreAESendEventNoReturnValue(
  321.                     const AEIdleUPP pIdleProcUPP,
  322.                     const AppleEvent *pAppleEvent )
  323. {
  324.     OSErr        anErr = noErr;
  325.     AppleEvent    theReply = {typeNull, nil};
  326.     AESendMode    sendMode;
  327.  
  328.     if (nil == pIdleProcUPP)
  329.         sendMode = kAENoReply;
  330.     else
  331.         sendMode = kAEWaitReply;
  332.  
  333.     anErr = AESend( pAppleEvent, &theReply, sendMode, kAENormalPriority, kNoTimeOut, pIdleProcUPP, nil );
  334.     if ((noErr == anErr) && (kAEWaitReply == sendMode))
  335.         anErr = MoreAEGetHandlerError(&theReply);
  336.  
  337.     (void) AEDisposeDesc( &theReply );
  338.     
  339.     return anErr;
  340. }//end MoreAESendEventNoReturnValue
  341. /********************************************************************************
  342.     Send the provided AppleEvent using the provided idle function.
  343.     Return data (at pDataPtr) of type pDesiredType
  344.  
  345.     pIdleProcUPP    input:    The idle function to use when sending the event.
  346.     pAppleEvent        input:    The event to be sent.
  347.     theValue        output:    The value returned by the event.
  348.  
  349.     RESULT CODES
  350.     ____________
  351.     noErr               0    No error    
  352.     paramErr         -50    No idle function provided
  353.  
  354.     and any other error that can be returned by AESend or the handler
  355.     in the application that gets the event.
  356.     ____________
  357. */
  358. pascal OSErr MoreAESendEventReturnData(
  359.                         const AEIdleUPP        pIdleProcUPP,
  360.                         const AppleEvent    *pAppleEvent,
  361.                         DescType            pDesiredType,
  362.                         DescType*            pActualType,
  363.                         void*                 pDataPtr,
  364.                         Size                pMaximumSize,
  365.                         Size                 *pActualSize)
  366. {
  367.     OSErr anErr = noErr;
  368.  
  369.     //    No idle function is an error, since we are expected to return a value
  370.     if (pIdleProcUPP == nil)
  371.         anErr = paramErr;
  372.     else
  373.     {
  374.         AppleEvent theReply = {typeNull, nil};
  375.         AESendMode sendMode = kAEWaitReply;
  376.  
  377.         anErr = AESend(pAppleEvent, &theReply, sendMode, kAENormalPriority, kNoTimeOut, pIdleProcUPP, nil);
  378.         //    [ Don't dispose of the event, it's not ours ]
  379.         if (anErr == noErr)
  380.         {
  381.             anErr = MoreAEGetHandlerError(&theReply);
  382.  
  383.             if (!anErr && theReply.descriptorType != typeNull)
  384.             {
  385.                 anErr = AEGetParamPtr(&theReply, keyDirectObject, pDesiredType,
  386.                             pActualType, pDataPtr, pMaximumSize, pActualSize);
  387.             }
  388.             AEDisposeDesc(&theReply);
  389.         }
  390.     }
  391.     return anErr;
  392. }    // MoreAESendEventReturnData
  393. /********************************************************************************
  394.     Send the provided AppleEvent using the provided idle function.
  395.     Return a SInt16 (typeSmallInteger).
  396.  
  397.     pIdleProcUPP    input:    The idle function to use when sending the event.
  398.     pAppleEvent        input:    The event to be sent.
  399.     theValue        output:    The value returned by the event.
  400.     
  401.     RESULT CODES
  402.     ____________
  403.     noErr               0    No error    
  404.     paramErr         -50    No idle function provided
  405.     
  406.     and any other error that can be returned by AESend or the handler
  407.     in the application that gets the event.
  408.     ____________
  409. */
  410. pascal OSErr MoreAESendEventReturnSInt16(
  411.                         const AEIdleUPP pIdleProcUPP,
  412.                         const AppleEvent* pAppleEvent,
  413.                         SInt16* pValue)
  414. {
  415.     DescType            actualType;
  416.     Size                 actualSize;
  417.  
  418.     return MoreAESendEventReturnData(pIdleProcUPP,pAppleEvent,typeShortInteger,
  419.                 &actualType,pValue,sizeof(SInt16),&actualSize);
  420. }    // MoreAESendEventReturnSInt16
  421. /********************************************************************************
  422.     Send the provided AppleEvent using the provided idle function.
  423.     Returns a PString.
  424.  
  425.     pIdleProcUPP    input:    The idle function to use when sending the event.
  426.     pAppleEvent        input:    The event to be sent.
  427.     pStr255        output:    The value returned by the event.
  428.  
  429.     RESULT CODES
  430.     ____________
  431.     noErr               0    No error    
  432.     paramErr         -50    No idle function provided
  433.  
  434.     and any other error that can be returned by AESend or the handler
  435.     in the application that gets the event.
  436.     ____________
  437. */
  438.  
  439. pascal OSErr MoreAESendEventReturnPString(
  440.                         const AEIdleUPP pIdleProcUPP,
  441.                         const AppleEvent* pAppleEvent,
  442.                         Str255 pStr255)
  443. {
  444.     DescType            actualType;
  445.     Size                 actualSize;
  446.  
  447.     return MoreAESendEventReturnData(pIdleProcUPP,pAppleEvent,typePString,
  448.                 &actualType,pStr255,sizeof(Str255),&actualSize);
  449. }    // MoreAESendEventReturnSInt16
  450. #pragma mark ==> Functions for talking to ourselfs
  451. /********************************************************************************
  452.     Send an AppleEvent of the specified Class & ID to myself using the 
  453.     default idle function.
  454.  
  455.     pEventID        input:    The event to be sent.
  456.  
  457.     RESULT CODES
  458.     ____________
  459.     noErr               0    No error    
  460.  
  461.     and any other error that can be returned by AESend or the handler
  462.     in the application that gets the event.
  463.     ____________
  464. */
  465. pascal OSErr MoreAESendToSelfNoReturnValue(
  466.                 const AEEventClass pEventClass,
  467.                 const AEEventID pEventID)
  468. {
  469.     AppleEvent theEvent = {typeNull, nil};    //    If you always init AEDescs, it's always save to dispose of them.
  470.     OSErr anErr = noErr;
  471.  
  472.     if (nil == gAEIdleUPP)
  473.         gAEIdleUPP = NewAEIdleUPP(MoreAESimpleIdleFunction);
  474.  
  475.     anErr = MoreAECreateAppleEventSelfTarget(pEventClass,pEventID,&theEvent);
  476.     if (anErr == noErr)
  477.     {
  478.         AEDesc containerObj = {typeNull, nil};    // start with the null (application) container
  479.         AEDesc propertyObject =    {typeNull, nil};
  480.  
  481.         anErr = MoreAEOCreatePropertyObject(pSelection, &containerObj, &propertyObject);
  482.         if (anErr == noErr)
  483.         {
  484.             anErr = AEPutParamDesc(&theEvent, keyDirectObject, &propertyObject);
  485.  
  486.             if (anErr == noErr)
  487.                 anErr = MoreAESendEventNoReturnValue(gAEIdleUPP, &theEvent);
  488.  
  489.             AEDisposeDesc(&propertyObject);        //    Always dispose of objects as soon as you are done (helps avoid leaks)
  490.         }
  491.          AEDisposeDesc(&theEvent);            // always dispose of AEDescs when you are finished with them
  492.     }
  493.     return anErr;
  494. }
  495.  
  496. /********************************************************************************
  497.     Send an AppleEvent of the specified Class & ID to myself using the 
  498.     default idle function. Wait for a reply and extract a SInt16 result.
  499.  
  500.     pEventID        input:    The event class to be sent.
  501.     pEventID        input:    The event ID to be sent.
  502.     pValue            Output:    Where the return SInt16 will be stored.
  503.  
  504.     RESULT CODES
  505.     ____________
  506.     noErr               0    No error    
  507.  
  508.     and any other error that can be returned by AESend or the handler
  509.     in the application that gets the event.
  510.     ____________
  511. */
  512. pascal OSErr MoreAESendToSelfReturnSInt16(
  513.                 const AEEventClass pEventClass,
  514.                 const AEEventID pEventID,
  515.                 SInt16* pValue)
  516. {
  517.     AppleEvent theEvent = {typeNull, nil};    //    If you always init AEDescs, it's always save to dispose of them.
  518.     OSErr anErr = noErr;
  519.  
  520.     if (nil == gAEIdleUPP)
  521.         gAEIdleUPP = NewAEIdleUPP(MoreAESimpleIdleFunction);
  522.  
  523.     anErr = MoreAECreateAppleEventSelfTarget(pEventClass,pEventID,&theEvent);
  524.     if (anErr == noErr)
  525.     {
  526.         AEDesc containerObj = {typeNull, nil};    // start with the null (application) container
  527.         AEDesc propertyObject =    {typeNull, nil};
  528.  
  529.         anErr = MoreAEOCreatePropertyObject(pSelection, &containerObj, &propertyObject);
  530.         if (anErr == noErr)
  531.         {
  532.             anErr = AEPutParamDesc(&theEvent, keyDirectObject, &propertyObject);
  533.  
  534.             if (anErr == noErr)
  535.                 anErr = MoreAESendEventReturnSInt16(gAEIdleUPP, &theEvent, pValue);
  536.  
  537.             AEDisposeDesc(&propertyObject);    //    Always dispose of objects as soon as you are done (helps avoid leaks)
  538.         }
  539.         AEDisposeDesc(&theEvent);    // always dispose of AEDescs when you are finished with them
  540.     }
  541.     return anErr;
  542. }
  543.  
  544. /********************************************************************************
  545.     Send a get data (kAEGetData) AppleEvent to myself using the 
  546.     default idle function. Wait for a reply and extract a SInt16 result.
  547.  
  548.     pIdleProcUPP    input:    The idle function to use when sending the event.
  549.     pEventID        input:    The event to be sent.
  550.     pValue            Output:    Where the resulting SInt16 will be stored.
  551.  
  552.     RESULT CODES
  553.     ____________
  554.     noErr               0    No error    
  555.  
  556.     and any other error that can be returned by AESend or the handler
  557.     in the application that gets the event.
  558.     ____________
  559. */
  560. pascal OSErr MoreAETellSelfToGetSInt16Property(
  561.                 const DescType pPropType,
  562.                 SInt16* pValue)
  563. {
  564.     AppleEvent theEvent = {typeNull, nil};    //    If you always init AEDescs, it's always save to dispose of them.
  565.     OSErr anErr = noErr;
  566.  
  567.     if (nil == gAEIdleUPP)
  568.         gAEIdleUPP = NewAEIdleUPP(MoreAESimpleIdleFunction);
  569.  
  570.     anErr = MoreAECreateAppleEventSelfTarget(kAECoreSuite,kAEGetData,&theEvent);
  571.     if (anErr == noErr)
  572.     {
  573.         AEDesc containerObj = {typeNull, nil};    // start with the null (application) container
  574.         AEDesc propertyObject =    {typeNull, nil};
  575.  
  576.         anErr = MoreAEOCreatePropertyObject(pPropType, &containerObj, &propertyObject);
  577.         if (anErr == noErr)
  578.         {
  579.             anErr = AEPutParamDesc(&theEvent, keyDirectObject, &propertyObject);
  580.             if (anErr == noErr)
  581.                 anErr = MoreAESendEventReturnSInt16(gAEIdleUPP, &theEvent, pValue);
  582.  
  583.             AEDisposeDesc(&propertyObject);    //    Always dispose of objects as soon as you are done (helps avoid leaks)
  584.         }
  585.         AEDisposeDesc(&theEvent);    // always dispose of AEDescs when you are finished with them
  586.     }
  587.     return anErr;
  588. }
  589. /********************************************************************************
  590.     Send a get data (kAEGetData) AppleEvent to myself using the 
  591.     default idle function. Wait for a reply and extract a Str255 result.
  592.  
  593.     pIdleProcUPP    input:    The idle function to use when sending the event.
  594.     pEventID        input:    The event to be sent.
  595.     pValue            Output:    Where the resulting Str255 will be stored.
  596.  
  597.     RESULT CODES
  598.     ____________
  599.     noErr               0    No error    
  600.  
  601.     and any other error that can be returned by AESend or the handler
  602.     in the application that gets the event.
  603.     ____________
  604. */
  605. pascal OSErr MoreAETellSelfToGetStr255Property(
  606.                 const DescType pPropType,
  607.                 Str255 pValue)
  608. {
  609.     AppleEvent theEvent = {typeNull, nil};    //    If you always init AEDescs, it's always save to dispose of them.
  610.     OSErr anErr = noErr;
  611.  
  612.     if (nil == gAEIdleUPP)
  613.         gAEIdleUPP = NewAEIdleUPP(MoreAESimpleIdleFunction);
  614.  
  615.     anErr = MoreAECreateAppleEventSelfTarget(kAECoreSuite,kAEGetData,&theEvent);
  616.     if (anErr == noErr)
  617.     {
  618.         AEDesc containerObj = {typeNull, nil};    // start with the null (application) container
  619.         AEDesc propertyObject =    {typeNull, nil};
  620.  
  621.         anErr = MoreAEOCreatePropertyObject(pPropType, &containerObj, &propertyObject);
  622.         if (anErr == noErr)
  623.         {
  624.             anErr = AEPutParamDesc(&theEvent, keyDirectObject, &propertyObject);
  625.             if (anErr == noErr)
  626.                 anErr = MoreAESendEventReturnPString(gAEIdleUPP, &theEvent, pValue);
  627.  
  628.             AEDisposeDesc(&propertyObject);    //    Always dispose of objects as soon as you are done (helps avoid leaks)
  629.         }
  630.         AEDisposeDesc(&theEvent);    // always dispose of AEDescs when you are finished with them
  631.     }
  632.     return anErr;
  633. }
  634.  
  635. /********************************************************************************
  636.     Send a set data (kAESetData) AppleEvent to myself with a SInt16 parameter
  637.     and using the default idle function.
  638.  
  639.     pIdleProcUPP    input:    The idle function to use when sending the event.
  640.     pEventID        input:    The event to be sent.
  641.     pValue            Output:    Where the resulting SInt16 will be stored.
  642.  
  643.     RESULT CODES
  644.     ____________
  645.     noErr               0    No error    
  646.  
  647.     and any other error that can be returned by AESend or the handler
  648.     in the application that gets the event.
  649.     ____________
  650. */
  651. pascal OSErr MoreAETellSelfToSetSInt16Property(
  652.                 const DescType pPropType,
  653.                 SInt16 pValue)
  654. {
  655.     AppleEvent theEvent = {typeNull, nil};    //    If you always init AEDescs, it's always save to dispose of them.
  656.     OSErr anErr = noErr;
  657.  
  658.     if (nil == gAEIdleUPP)
  659.         gAEIdleUPP = NewAEIdleUPP(MoreAESimpleIdleFunction);
  660.  
  661.     anErr = MoreAECreateAppleEventSelfTarget(kAECoreSuite,kAESetData,&theEvent);
  662.     if (anErr == noErr)
  663.     {
  664.         AEDesc containerObj = {typeNull, nil};    // start with the null (application) container
  665.         AEDesc propertyObject =    {typeNull, nil};
  666.  
  667.         anErr = MoreAEOCreatePropertyObject(pPropType, &containerObj, &propertyObject);
  668.         if (anErr == noErr)
  669.         {
  670.             anErr = AEPutParamDesc(&theEvent, keyDirectObject, &propertyObject);
  671.             AEDisposeDesc(&propertyObject);
  672.             if (anErr == noErr)
  673.             {
  674.                 anErr = AEPutParamPtr(&theEvent, keyAEData, typeSInt16, &pValue, sizeof(SInt16));
  675.                 if (anErr == noErr)
  676.                     anErr = MoreAESendEventNoReturnValue(gAEIdleUPP, &theEvent);
  677.             }
  678.         }
  679.         AEDisposeDesc(&theEvent);    // always dispose of AEDescs when you are finished with them
  680.     }
  681.     return anErr;
  682. }
  683.  
  684. /********************************************************************************
  685.     Send a set data (kAESetData) AppleEvent to myself with a SInt16 parameter
  686.     and using the default idle function.
  687.  
  688.     pIdleProcUPP    input:    The idle function to use when sending the event.
  689.     pEventID        input:    The event to be sent.
  690.     pValue            Output:    Where the resulting Str255 will be stored.
  691.  
  692.     RESULT CODES
  693.     ____________
  694.     noErr               0    No error    
  695.  
  696.     and any other error that can be returned by AESend or the handler
  697.     in the application that gets the event.
  698.     ____________
  699. */
  700. pascal OSErr MoreAETellSelfToSetStr255Property(
  701.                 const DescType pPropType,
  702.                 Str255 pValue)
  703. {
  704.     AppleEvent theEvent = {typeNull, nil};    //    If you always init AEDescs, it's always save to dispose of them.
  705.     OSErr anErr = noErr;
  706.  
  707.     if (nil == gAEIdleUPP)
  708.         gAEIdleUPP = NewAEIdleUPP(MoreAESimpleIdleFunction);
  709.  
  710.     anErr = MoreAECreateAppleEventSelfTarget(kAECoreSuite,kAESetData,&theEvent);
  711.     if (anErr == noErr)
  712.     {
  713.         AEDesc containerObj = {typeNull, nil};    // start with the null (application) container
  714.         AEDesc propertyObject =    {typeNull, nil};
  715.  
  716.         anErr = MoreAEOCreatePropertyObject(pPropType, &containerObj, &propertyObject);
  717.         if (anErr == noErr)
  718.         {
  719.             anErr = AEPutParamDesc(&theEvent, keyDirectObject, &propertyObject);
  720.             AEDisposeDesc(&propertyObject);
  721.             if (anErr == noErr)
  722.             {
  723.                 anErr = AEPutParamPtr(&theEvent, keyAEData, typePString, pValue, pValue[0] + 1);
  724.                 if (anErr == noErr)
  725.                     anErr = MoreAESendEventNoReturnValue(gAEIdleUPP, &theEvent);
  726.             }
  727.         }
  728.         AEDisposeDesc(&theEvent);    // always dispose of AEDescs when you are finished with them
  729.     }
  730.     return anErr;
  731. }
  732. //*******************************************************************************
  733. #pragma mark ==> Misc. AE utility functions •
  734. //*******************************************************************************
  735. // Appends each of the items in pSourceList to the pDestList.
  736. extern pascal OSStatus MoreAEAppendListToList(const AEDescList *pSourceList, AEDescList *pDestList)
  737. {
  738.     OSStatus  err;
  739.     AEKeyword junkKeyword;
  740.     SInt32    listCount;
  741.     SInt32    listIndex;
  742.     AEDesc      thisValue;
  743.  
  744.     MoreAssertQ(pSourceList != nil);
  745.     MoreAssertQ(pDestList   != nil);
  746.  
  747.     err = AECountItems(pSourceList, &listCount);
  748.     if (err == noErr) {
  749.         for (listIndex = 1; listIndex <= listCount; listIndex++) {
  750.             MoreAENullDesc(&thisValue);
  751.             
  752.             err = AEGetNthDesc(pSourceList, listIndex, typeWildCard, &junkKeyword, &thisValue);
  753.             if (err == noErr) {
  754.                 err = AEPutDesc(pDestList, 0, &thisValue);
  755.             }
  756.             
  757.             MoreAEDisposeDesc(&thisValue);
  758.             if (err != noErr) {
  759.                 break;
  760.             }
  761.         }
  762.     }
  763.  
  764.     return err;
  765. }
  766.  
  767. //*******************************************************************************
  768. // This routine takes a result descriptor and an error.
  769. // If there is a result to add to the reply it makes sure the reply isn't
  770. // NULL itself then adds the error to the reply depending on the error
  771. // and the type of result.
  772. pascal OSErr MoreAEMoreAESetReplyErrorNumber (OSErr pOSErr, AppleEvent *pAEReply)
  773. {
  774.     OSErr err = noErr;
  775.  
  776.     if (pAEReply->dataHandle)
  777.     {
  778.         if (!MoreAssert (pAEReply->descriptorType == typeAppleEvent))
  779.             err = paramErr;
  780.         else
  781.             err = AEPutParamPtr (pAEReply,keyErrorNumber,typeShortInteger,&pOSErr,sizeof(pOSErr));
  782.     }
  783.  
  784.     return err;
  785. }
  786. //*******************************************************************************
  787. // This routine takes a result descriptor, a reply descriptor and an error.
  788. // If there is a result to add to the reply it makes sure the reply isn't
  789. // NULL itself then adds the result to the reply depending on the error
  790. // and the type of result.
  791.  
  792. pascal OSErr MoreAEAddResultToReply(AEDesc* pResult, AEDesc* pAEReply, OSErr error)
  793. {
  794.     OSErr    anErr;
  795.  
  796.     // Check that the pAEReply is not NULL and there is a result to put in it  
  797.     if (typeNull == pAEReply->descriptorType || typeNull == pResult->descriptorType)
  798.         return (error);
  799.     
  800.     if (noErr == error)
  801.         anErr = AEPutParamDesc(pAEReply, keyDirectObject, pResult);
  802.     else
  803.     {
  804.         switch (pResult->descriptorType)
  805.         {
  806.             case typeInteger:
  807.                 anErr = AEPutParamDesc(pAEReply, keyErrorNumber, pResult);
  808.                 break;
  809.                 
  810.             case typeChar:
  811.                 anErr = AEPutParamDesc(pAEReply, keyErrorString, pResult);
  812.                 break;
  813.                 
  814.             default:
  815.                 anErr = errAETypeError;
  816.         }
  817.         
  818.         if (noErr == anErr)
  819.             anErr = error;        // Don't loose that error
  820.     }
  821.     return (anErr);
  822. }
  823. // ----------------------------------------------------------------------
  824. //    Name:        MoreAEGotRequiredParams
  825. //    Function:    Checks that all parameters defined as 'required' have been read
  826. // ----------------------------------------------------------------------
  827. pascal OSErr    MoreAEGotRequiredParams(const AppleEvent *theAppleEvent)
  828. {
  829.     DescType    returnedType;
  830.     Size        actualSize;
  831.     OSErr        anErr;
  832.     
  833.     // look for the keyMissedKeywordAttr, just to see if it's there
  834.     
  835.     anErr = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  836.                                                 &returnedType, NULL, 0, &actualSize);
  837.     
  838.     switch (anErr)
  839.     {
  840.         case errAEDescNotFound:        // attribute not there means we
  841.             anErr = noErr;            // got all required parameters.
  842.             break;
  843.             
  844.         case noErr:                    // attribute there means missed
  845.             anErr = errAEParamMissed;    // at least one parameter.
  846.             break;
  847.             
  848.         // default:        pass on unexpected error in looking for the attribute
  849.     }
  850.     return (anErr);
  851. } // GotReqiredParams
  852. /********************************************************************************
  853.     Takes a reply event checks it for any errors that may have been returned
  854.     by the event handler. A simple function, in that it only returns the error
  855.     number. You can often also extract an error string and three other error
  856.     parameters from a reply event.
  857.     
  858.     Also see:
  859.         IM:IAC for details about returned error strings.
  860.         AppleScript developer release notes for info on the other error parameters.
  861.     
  862.     pAEReply        input:    The reply event to be checked.
  863.  
  864.     RESULT CODES
  865.     ____________
  866.     noErr                    0    No error    
  867.     ????                    ??    Pretty much any error, depending on what the
  868.                                 event handler returns for it's errors.
  869. */
  870. pascal    OSErr    MoreAEGetHandlerError( const AppleEvent *pAEReply )
  871. {
  872.     OSErr        anErr = noErr;
  873.     OSErr        handlerErr;
  874.     
  875.     DescType    actualType;
  876.     long        actualSize;
  877.     
  878.     if ( pAEReply->descriptorType != typeNull )    // there's a reply, so there may be an error
  879.     {
  880.         OSErr    getErrErr = noErr;
  881.         
  882.         getErrErr = AEGetParamPtr( pAEReply, keyErrorNumber, typeShortInteger, &actualType,
  883.                                     &handlerErr, sizeof( OSErr ), &actualSize );
  884.         
  885.         if ( getErrErr != errAEDescNotFound )    // found an errorNumber parameter
  886.         {
  887.             anErr = handlerErr;                    // so return it's value
  888.         }
  889.     }
  890.     return anErr;
  891. }//end MoreAEGetHandlerError
  892. /********************************************************************************
  893.     Get the class and ID from an AppleEvent.
  894.  
  895.     pAppleEvent        input:    The event to get the class and ID from.
  896.     pAEEventClass       output:    The event's class.
  897.     pAEEventID           output:    The event's ID.
  898.     
  899.     RESULT CODES
  900.     ____________
  901.     noErr                        0    No error    
  902.     memFullErr                 -108    Not enough room in heap zone    
  903.     errAEDescNotFound         -1701    Descriptor record was not found    
  904.     errAENotAEDesc            -1704    Not a valid descriptor record    
  905.     errAEReplyNotArrived    -1718    Reply has not yet arrived    
  906. */    
  907. pascal OSErr MoreAEExtractClassAndID ( const AppleEvent *pAppleEvent, AEEventClass *pAEEventClass, AEEventID *pAEEventID )
  908. {
  909.     DescType    actualType;
  910.     Size        actualSize;
  911.     OSErr        anErr;
  912.  
  913.     anErr = AEGetAttributePtr( pAppleEvent, keyEventClassAttr, typeType, &actualType,
  914.                                 pAEEventClass, sizeof( pAEEventClass ), &actualSize );
  915.     if ( anErr == noErr )
  916.     {
  917.         anErr = AEGetAttributePtr( pAppleEvent, keyEventIDAttr, typeType, &actualType,
  918.                                     pAEEventID, sizeof( pAEEventID ), &actualSize );
  919.     }
  920.     return ( anErr );
  921. }//end ExtractClassAndID
  922. /********************************************************************************
  923.     A very simple idle function. It simply ignors any event it receives,
  924.     returns 30 for the sleep time and nil for the mouse region.
  925.     
  926.     Your application should supply an idle function that handles any events it
  927.     might receive. This is especially important if your application has any windows.
  928.     
  929.     Also see:
  930.         IM:IAC for details about idle functions.
  931.         Pending Update Perils technote for more about handling low-level events.
  932. */    
  933. pascal    Boolean    MoreAESimpleIdleFunction( EventRecord *event,
  934.                                        long *sleepTime,
  935.                                        RgnHandle *mouseRgn )
  936. {
  937. #pragma unused( event )
  938.     *sleepTime = 30;
  939.     *mouseRgn = nil;
  940.     
  941.     return ( false );
  942. }//end MoreAESimpleIdleFunction
  943. /********************************************************************************
  944.     Is the Apple Event Manager present.
  945.     
  946.     RESULT CODES
  947.     ____________
  948.     true    The Apple Event Manager is present
  949.     false    It isn't
  950. */
  951. pascal Boolean MoreAEHasAppleEvents(void)
  952. {
  953.     OSErr    anErr = noErr;
  954.     
  955.     static    long        gHasAppleEvents = kFlagNotSet;
  956.     
  957.     if ( gHasAppleEvents == kFlagNotSet )
  958.     {
  959.         long    response;
  960.         
  961.         if ( Gestalt( gestaltAppleEventsAttr, &response ) == noErr )
  962.             gHasAppleEvents = ( response & (1L << gestaltAppleEventsPresent) ) != 0;
  963.     }
  964.     return gHasAppleEvents;
  965. }//end MoreAEHasAppleEvents
  966. //*******************************************************************************
  967. // Did this AppleEvent come from the Finder?
  968. pascal OSErr MoreAEIsSenderFinder (const AppleEvent *pAppleEvent, Boolean *pIsFinder)
  969. {
  970.     OSErr                    err = noErr;
  971.     DescType                actualType;
  972.     ProcessSerialNumber        senderPSN;
  973.     Size                    actualSize;
  974.  
  975.     if (!MoreAssert (pAppleEvent && pIsFinder))                            return paramErr;
  976.     if (!MoreAssert (pAppleEvent->descriptorType == typeAppleEvent))    return paramErr;
  977.     if (!MoreAssert (pAppleEvent->dataHandle))                            return paramErr;
  978.  
  979.     err = AEGetAttributePtr (pAppleEvent, keyAddressAttr, typeProcessSerialNumber, &actualType,
  980.         (Ptr) &senderPSN, sizeof (senderPSN), &actualSize);
  981.     if (MoreAssert (err == noErr))
  982.     {
  983.         if (!MoreAssert (actualType == typeProcessSerialNumber))
  984.             err = paramErr;
  985.         else if (!MoreAssert (actualSize == sizeof (senderPSN)))
  986.             err = paramErr;
  987.         else
  988.         {
  989.             ProcessInfoRec processInfo;
  990.  
  991.             if (!(err = MoreProcGetProcessInformation (&senderPSN,&processInfo)))
  992.             {
  993.                 *pIsFinder = (    processInfo.processSignature == kFinderProcessSignature && 
  994.                                 processInfo.processType == kFinderProcessType);
  995.             }
  996.         }        
  997.     }
  998.  
  999.     return err;
  1000. }
  1001. #pragma mark ==> AEDesc Constructor & Destructor •
  1002. //*******************************************************************************
  1003. // Initialises desc to the null descriptor (typeNull, nil).
  1004. pascal void MoreAENullDesc(AEDesc *desc)
  1005. {
  1006.     MoreAssertQ(desc != nil);
  1007.  
  1008.     desc->descriptorType = typeNull;
  1009.     desc->dataHandle     = nil;
  1010. }
  1011.  
  1012. //*******************************************************************************
  1013. // Disposes of desc and initialises it to the null descriptor.
  1014. pascal void MoreAEDisposeDesc(AEDesc *desc)
  1015. {
  1016.     OSStatus junk;
  1017.  
  1018.     MoreAssertQ(desc != nil);
  1019.     
  1020.     junk = AEDisposeDesc(desc);
  1021.     MoreAssertQ(junk == noErr);
  1022.  
  1023.     desc->descriptorType = typeNull;
  1024.     desc->dataHandle     = nil;
  1025. }
  1026.  
  1027. //*******************************************************************************
  1028. #pragma mark ==> AEDesc Data Accessors for 68K •
  1029. //*******************************************************************************
  1030. // These routines are only implemented in PreCarbon.o for PowerPC
  1031. // So for 68K we had to write our own versions
  1032.  
  1033. #if TARGET_CPU_68K || !ACCESSOR_CALLS_ARE_FUNCTIONS
  1034.  
  1035. pascal Size AEGetDescDataSize(    const AEDesc* pAEDesc)
  1036. {
  1037.     return GetHandleSize(pAEDesc->dataHandle);
  1038. }
  1039.  
  1040. pascal OSErr AEGetDescData(    const AEDesc*    pAEDesc,
  1041.                 void*            pDataPtr,
  1042.                 Size             pMaxSize)
  1043. {
  1044.     Size copySize = AEGetDescDataSize(pAEDesc);
  1045.  
  1046.     if ((nil == pAEDesc) || (nil == pDataPtr))
  1047.         return paramErr;
  1048.  
  1049.     if (pMaxSize < copySize)
  1050.         copySize = pMaxSize;
  1051.  
  1052.     BlockMoveData(*pAEDesc->dataHandle,pDataPtr,copySize);
  1053.  
  1054.     return noErr;
  1055. }
  1056. #endif TARGET_CPU_68K || !ACCESSOR_CALLS_ARE_FUNCTIONS
  1057. //*******************************************************************************
  1058. #pragma mark ==> Get Data From Descriptors •
  1059. /********************************************************************************
  1060.     This is the generic routine that all the other's use instead of
  1061.     duplicating all this code unnecessarily.
  1062.  
  1063.     pAEDesc        input:    The descriptor we want the data from
  1064.     pDestPtr        output:    Where we want to store the data from this desc.
  1065.     pMaxSize        input:    The maxium amount of data we can store.
  1066.     pActualSize    output:    The actual amount of data stored.
  1067.  
  1068.     RESULT CODES
  1069.     ____________
  1070.     noErr               0    No error    
  1071.     ____________
  1072. */
  1073. pascal void MoreAEGetRawDataFromDescriptor(const AEDesc* pAEDesc,
  1074.                              Ptr     pDestPtr,
  1075.                              Size    pMaxSize,
  1076.                              Size    *pActualSize)
  1077. {
  1078.     Size copySize;
  1079.  
  1080.     if (pAEDesc->dataHandle) 
  1081.     {
  1082.         *pActualSize = AEGetDescDataSize(pAEDesc);
  1083.  
  1084.         if (*pActualSize < pMaxSize)
  1085.             copySize = *pActualSize;
  1086.         else
  1087.             copySize = pMaxSize;
  1088.  
  1089.          AEGetDescData(pAEDesc,pDestPtr,copySize);
  1090.     }
  1091.     else
  1092.         *pActualSize = 0;
  1093. }
  1094. /********************************************************************************
  1095.     Extract a pascal string a descriptor.
  1096.  
  1097.   pAEDesc        input:    The descriptor we want the data from
  1098.   pStringPtr    output:    Where we want to store the pascal string
  1099.  
  1100.   RESULT CODES
  1101.   ____________
  1102.   noErr               0    No error    
  1103.   ____________
  1104. */
  1105. pascal OSErr MoreAEGetPStringFromDescriptor(const AEDesc* pAEDesc, StringPtr pStringPtr)
  1106. {
  1107.     Size         stringSize;
  1108.     AEDesc       resultDesc = {typeNull, NULL};
  1109.     OSErr        anErr;
  1110.     
  1111.     anErr = AECoerceDesc(pAEDesc, typeChar, &resultDesc);
  1112.     if (noErr != anErr) goto done;
  1113.     
  1114.     pStringPtr[0] = 0;
  1115.     
  1116.     MoreAEGetRawDataFromDescriptor(&resultDesc, (Ptr) &pStringPtr[1], 255, &stringSize);
  1117.     if (stringSize <= 255) 
  1118.         pStringPtr[0] = stringSize;
  1119.     else
  1120.         anErr = errAECoercionFail;
  1121.  
  1122. done:        
  1123.     if (resultDesc.dataHandle) 
  1124.         AEDisposeDesc(&resultDesc);
  1125.         
  1126.     return(anErr);
  1127. }
  1128. /********************************************************************************
  1129.     Extract a C string from the descriptor.
  1130.  
  1131.   pAEDesc        input:    The descriptor we want the data from
  1132.   pStringPtr    output:    Where we want to store the C string
  1133.  
  1134.   RESULT CODES
  1135.   ____________
  1136.   noErr               0    No error    
  1137.   ____________
  1138. */
  1139. pascal OSErr MoreAEGetCStringFromDescriptor(const AEDesc* pAEDesc, char* pCharPtr)
  1140. {
  1141.     Size         stringSize;
  1142.     AEDesc       resultDesc = {typeNull, NULL};
  1143.     OSErr        anErr;
  1144.     
  1145.     anErr = AECoerceDesc(pAEDesc, typeChar, &resultDesc);
  1146.     if (noErr == anErr)
  1147.     {
  1148.         MoreAEGetRawDataFromDescriptor(&resultDesc, pCharPtr, 256, &stringSize);
  1149.         pCharPtr[stringSize] = 0;
  1150.     }
  1151.     if (resultDesc.dataHandle) 
  1152.         AEDisposeDesc(&resultDesc);
  1153.         
  1154.     return (anErr);
  1155. }
  1156.  
  1157. /********************************************************************************
  1158.     Extract a short from a descriptor.
  1159.  
  1160.   pAEDesc        input:    The descriptor we want the data from
  1161.   pStringPtr    output:    Where we want to store the short.
  1162.  
  1163.   RESULT CODES
  1164.   ____________
  1165.   noErr               0    No error    
  1166.   ____________
  1167. */
  1168.  
  1169. pascal OSErr MoreAEGetShortFromDescriptor(const AEDesc* pAEDesc, SInt16 *pResult)
  1170. {
  1171.     OSErr   myErr;
  1172.     OSErr   ignoreErr;
  1173.     Size    intSize;
  1174.     AEDesc  resultDesc;
  1175.  
  1176.     *pResult = 0;
  1177.  
  1178.     myErr = AECoerceDesc(pAEDesc,typeShortInteger,&resultDesc);
  1179.  
  1180.     if (myErr==noErr) 
  1181.     {
  1182.         MoreAEGetRawDataFromDescriptor(&resultDesc,(Ptr) pResult,2,&intSize);
  1183.         if (intSize>2) 
  1184.             myErr = errAECoercionFail;
  1185.     }
  1186.  
  1187.     if (resultDesc.dataHandle) 
  1188.         ignoreErr = AEDisposeDesc(&resultDesc);
  1189.  
  1190.     return(myErr);
  1191. }
  1192.  
  1193. /********************************************************************************
  1194.     Extract a Boolean from a descriptor.
  1195.  
  1196.   pAEDesc        input:    The descriptor we want the data from
  1197.   pStringPtr    output:    Where we want to store the boolean.
  1198.  
  1199.   RESULT CODES
  1200.   ____________
  1201.   noErr               0    No error    
  1202.   ____________
  1203. */
  1204.  
  1205. pascal OSErr MoreAEGetBooleanFromDescriptor(const AEDesc* pAEDesc,Boolean *pResult)
  1206. {
  1207.     OSErr  myErr;
  1208.     OSErr  ignoreErr;
  1209.     Size   boolSize;
  1210.     AEDesc resultDesc;
  1211.  
  1212.     *pResult = false;
  1213.     myErr = AECoerceDesc(pAEDesc,typeBoolean,&resultDesc);
  1214.  
  1215.     if (myErr==noErr) 
  1216.     {
  1217.         MoreAEGetRawDataFromDescriptor(&resultDesc,(Ptr)pResult,
  1218.             sizeof(Boolean),&boolSize);
  1219.         if (boolSize > sizeof(Boolean)) 
  1220.             myErr = errAECoercionFail;
  1221.     }
  1222.  
  1223.     if (resultDesc.dataHandle) 
  1224.         ignoreErr = AEDisposeDesc(&resultDesc);
  1225.  
  1226.     return(myErr);
  1227. }
  1228.  
  1229. /********************************************************************************
  1230.     Extract a long from a descriptor.
  1231.  
  1232.   pAEDesc        input:    The descriptor we want the data from
  1233.   pStringPtr    output:    Where we want to store the long.
  1234.  
  1235.   RESULT CODES
  1236.   ____________
  1237.   noErr               0    No error    
  1238.   ____________
  1239. */
  1240.  
  1241. pascal OSErr MoreAEGetLongFromDescriptor(const AEDesc* pAEDesc, long   *pResult)
  1242. {
  1243.     OSErr   myErr;
  1244.     OSErr   ignoreErr;
  1245.     Size    intSize;
  1246.     AEDesc  resultDesc;
  1247.  
  1248.     *pResult = 0;
  1249.     myErr = AECoerceDesc(pAEDesc,typeLongInteger,&resultDesc);
  1250.  
  1251.     if (myErr==noErr) 
  1252.     {
  1253.         MoreAEGetRawDataFromDescriptor(&resultDesc,(Ptr)pResult,4,&intSize);
  1254.         if (intSize>4) 
  1255.             myErr = errAECoercionFail;
  1256.     }
  1257.     if (resultDesc.dataHandle) 
  1258.     ignoreErr = AEDisposeDesc(&resultDesc);
  1259.  
  1260.     return(myErr);
  1261. } /*MoreAEGetLongIntFromDescriptor*/
  1262.  
  1263. /********************************************************************************
  1264.     Extract a OSType from a descriptor.
  1265.  
  1266.   pAEDesc        input:    The descriptor we want the data from
  1267.   pResult        output:    Where we want to store the OSType.
  1268.  
  1269.   RESULT CODES
  1270.   ____________
  1271.   noErr               0    No error    
  1272.   ____________
  1273. */
  1274.  
  1275. pascal OSErr MoreAEGetOSTypeFromDescriptor(const AEDesc* pAEDesc, OSType   *pResult)
  1276. {
  1277.     return (MoreAEGetLongFromDescriptor(pAEDesc,(long *)pResult));
  1278. } /*MoreAEGetOSTypeFromDescriptor*/
  1279.  
  1280. extern pascal OSStatus MoreAECopyDescriptorDataToHandle(const AEDesc *desc, Handle *descData)
  1281.     // see comment in header.
  1282. {
  1283.     OSStatus err;
  1284.     OSStatus junk;
  1285.     
  1286.     MoreAssertQ(desc      != nil);
  1287.     MoreAssertQ(descData  != nil);
  1288.     MoreAssertQ(*descData == nil);
  1289.     
  1290.     *descData = NewHandle(AEGetDescDataSize(desc));
  1291.     err = MoreMemError(*descData);
  1292.     if (err == noErr) {
  1293.         HLock(*descData);
  1294.         junk = AEGetDescData(desc, **descData, AEGetDescDataSize(desc));
  1295.         MoreAssertQ(junk == noErr);
  1296.         HUnlock(*descData);
  1297.     }
  1298.     return err;
  1299. }
  1300.  
  1301. extern pascal Boolean MoreAEIsMissingValue(const AEDesc *desc)
  1302.     // See comment in header.
  1303. {
  1304.     DescType missing;
  1305.     
  1306.     return (desc->descriptorType == typeType)
  1307.             && (AEGetDescDataSize(desc) == sizeof(missing))
  1308.             && (AEGetDescData(desc, &missing, sizeof(missing)) == noErr)
  1309.             && (missing == cMissingValue);
  1310. }
  1311.  
  1312. extern pascal OSStatus MoreAECreateMissingValue(AEDesc *desc)
  1313.     // See comment in header.
  1314. {
  1315.     const static DescType missingValue = cMissingValue;
  1316.     
  1317.     return AECreateDesc(typeType, &missingValue, sizeof(missingValue), desc);
  1318. }
  1319.